'''
Back end for the coffee bar workflow (Winter Intensives Lab 5)

This file is where you "wire" together the steps in the process.
'''

# these next few lines import some of the WMP functions we will
# use in this file.
from backend.backend import Backend
from workflow.task import Task
from collections import OrderedDict;
# from workflow.result import Result # uncomment this line if using Result objects (not needed in simple workflow)
from workflow.flowData import Status

class RetailBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "RugsWorkflow")  # be sure you update the workflow name

    def wire(self):

        # add a line of code here to register the step where a drink order is taken.
        self.register_result_listener("Customer", "PlaceOrder", self.place_order)
        # add a line of code here to register the step where a drink is prepared.
        self.register_result_listener("Shipper", "ShipOrder", self.order_shipped)
#        self.register_result_listener("Biller", "ObserveOrder", self.observe_order)

    def place_order(self, results):
        
        for result in results:  # repeat the following actions for each result
            task = Task.construct_from_result(result, "Shipper", "ShipOrder", 
                                              data=OrderedDict(), status=Status.NEW,
                                              uid=None, copy=["FullName","Email","Address","City"])
            self.workflow.add(task) # add the new task to the workflow
            task = Task.construct_from_result(result, "Biller", "ObserveOrder")
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(result, Status.COMPLETE)

    def order_shipped(self, results):
        for result in results:  # repeat the following actions for each result
            self.workflow.update_status(result, Status.COMPLETE)

if __name__ == '__main__':
    backend = RetailBackend()